package com.shiva.oms.biz;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sf.shiva.oms.common.util.CollectionUtil;
import com.shiva.oms.logview.domain.HistoryFileRecord;

/**
 * 
 * 用户之前解析过的文件记录
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年4月23日         LA.01367604           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author LA.01367604
 * @version 1.0
 */
public class HistoryFileRecordBiz extends AbstractCustomTemp<HistoryFileRecord> {

    private static final Logger logger = LoggerFactory.getLogger(HistoryFileRecordBiz.class);

    private static final String fileDir = "logv_conf";
    private static final String fileName = "history_file.temp";
    private static final int MAX_KEEP = 5;
    private static final String ENTITY_SPLIT = ";";
    private static final String ENTITY_ATTR_SPLIT = ",";
    private File file = new File(CURRENT_DIR + FILE_SEP + fileDir + FILE_SEP + fileName);
    private List<HistoryFileRecord> fileList;
    private static final HistoryFileRecordBiz instance = new HistoryFileRecordBiz();

    private HistoryFileRecordBiz() {}

    public static HistoryFileRecordBiz getInstance() {
        return instance;
    }

    public List<HistoryFileRecord> getList() {
        if (fileList == null) {
            fileList = super.init(fileDir, fileName, file);
        }
        return fileList;
    }
    
    @Override
    protected List<HistoryFileRecord> transStr2List(String str) {
        List<HistoryFileRecord> list = new ArrayList<>();
        if (StringUtils.isNotBlank(str)) {
            String[] datas = str.split(ENTITY_SPLIT);
            for (int i = 0; i < datas.length; i++) {
                String[] temp = datas[i].split(ENTITY_ATTR_SPLIT);
                list.add(new HistoryFileRecord(temp[0], temp[1]));
            }
        }
        return list;
    }

    public void addFileRec(HistoryFileRecord rec) {
        if (fileList == null) {
            fileList = super.init(fileDir, fileName, file);
        }
        fileList = fileList.stream().filter(data -> !data.equals(rec)).collect(Collectors.toList());
        fileList.add(rec);
        if (fileList.size() > MAX_KEEP) {// 超过最大记录数则删除最早的记录
            fileList.remove(0);
        }
        super.saveAll(fileList, file);
    }
    
    public HistoryFileRecord getFileRecByName(String fileName) {
        if (CollectionUtil.isEmpty(fileList)) {
            return null;
        }
        return fileList.stream().filter(elem -> fileName.equals(elem.getFileName())).findFirst().orElse(null);
    }

    /**
     * 保存格式是：文件路径1,文件名1;文件路径2,文件名2
     */
    @Override
    protected String transList2Str(List<HistoryFileRecord> list) {
        return list.stream()
                .map(data -> data.getFilePath() + ENTITY_ATTR_SPLIT + data.getFileName())
                .collect(Collectors.joining(ENTITY_SPLIT));
    }

    public static void main(String[] args) {
        HistoryFileRecordBiz biz = HistoryFileRecordBiz.getInstance();
        List<HistoryFileRecord> list = biz.getList();
        HistoryFileRecord rec = new HistoryFileRecord("/ee/ss/", "name3");
        biz.addFileRec(rec);
        list = biz.getList();
        System.out.println(list.size());
    }

}
